/* 
 * File:   serverUDP.c
 * Author: dannv
 *
 * Created on October 23, 2009, 10:06 AM
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>

#define SERV_UDP_PORT 12356
#define SIZE 1024

int softerror(int);
void serve(int, struct sockaddr_in);

/*
 * Afficher l'erreur et quiter le serveur
 */
void err_stop(msg) char *msg;
{
    perror(msg);
    exit(-1);
}

/*
 *
 */
int main(int argc, char** argv) {

    int sockfd;
    struct sockaddr_in serv_addr, client_addr;

    // Ouvrir un udp socket
    if ( (sockfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {
        err_stop("Open datagram socket");
    }

    // 	Liez notre adresse locale afin que le client ne peut nous l'envoyer
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(SERV_UDP_PORT);

    if (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
        err_stop("Bind address to socket");
    }
    
    serve(sockfd, client_addr);

    return (EXIT_SUCCESS);
}

void serve(int sockfd, struct sockaddr_in client_addr) {
    socklen_t addr_size;
    char buff[SIZE];

    for (;;) {
        addr_size = sizeof(client_addr);        
        // Recevoir ce que le client a envoyé
        int len;
        if ((len = recvfrom(sockfd, buff, SIZE, 0, (struct sockaddr*)&client_addr, (socklen_t*)&addr_size)) < 0) {
            perror("Receive from client");
            continue;
        }
        buff[len] = '\0';
        if (strcmp(buff, "Bonjour") != 0) {
            sprintf(buff, "%s\n" , "Vous devez dire Bonjour !!!");
            if (sendto(sockfd, buff, strlen(buff), 0, (struct sockaddr*)&client_addr, addr_size) != strlen(buff)) {
                perror("Send message to client");                              
            }
            continue;
        }

        int i;
        for (i = 0; i < 60; i++) {
            struct tm *tm;
            time_t now = time(0);
            tm = localtime(&now);
            sprintf(buff, "Il est %d:%d:%d !\n", tm->tm_hour, tm->tm_min, tm->tm_sec);            
            if (sendto(sockfd, buff, strlen(buff), 0, (struct sockaddr*)&client_addr, addr_size) != strlen(buff)) {
                perror("Server: Send to client");
                break;
            }
            sleep(1);
        }
        sprintf(buff, "Au revoir %s !\n", inet_ntoa(client_addr.sin_addr));
        if (sendto(sockfd, buff, strlen(buff), 0, (struct sockaddr*)&client_addr, addr_size) != strlen(buff)) {
            perror("Server: Send to client");
            continue;
        }
    }
    close(sockfd);
}

/*
 * Return 1 if error is recoverable and 0 if un recoverable
 */
int softerror(int num) {
    switch (num) {
        case EAGAIN:
        case ENOSR:
        case ENODEV:
        case ENOSPC:
        case ENOMEM:
        case ENXIO:
        case ENOBUFS:
            return 1;
        default:
            return 0;
    }
}

